<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            月度生产统计
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">计划日期起</div>
                    <div class="aui-list-item-input" onclick="timePicker('planDateStart');">
                        <input type="text" placeholder="选择日期" class="aui-input" id="planDateStart">
                    </div>
                </div>
            </li>
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">计划日期止</div>
                    <div class="aui-list-item-input" onclick="timePicker('planDateEnd');">
                        <input type="text" placeholder="选择日期" class="aui-input" id="planDateEnd">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="search()">查询</div>
    </div>
    <div id="echartBar"
        style="height: 500px;width: 100%;white-space: nowrap;overflow-x: auto;overflow-y: auto;-webkit-overflow-scrolling:touch;">
    </div>
    <!-- <div id="echartBar" style="width: 100%;">
    </div> -->
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/echarts.min.js"></script>
<script type="text/javascript">
    //初始化
    apiready = function () {

        //初始化当前日期
        crateDate();
    }

    //获取当前日期
    function crateDate() {
        var planDateStart = getCurrentMonthFirst();
        var planDateEnd = getCurrentMonthLast();
        $("#planDateStart").val(planDateStart);
        $("#planDateEnd").val(planDateEnd);

        search();
    }

    //获取当前月份第一天
    function getCurrentMonthFirst() {
        var date = new Date();
        date.setDate(1);
        var month = parseInt(date.getMonth() + 1);
        var day = date.getDate();
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        return date.getFullYear() + '-' + month + '-' + day;
    }

    //获取当前月份最后一天
    function getCurrentMonthLast() {
        var date = new Date();
        var currentMonth = date.getMonth();
        var nextMonth = ++currentMonth;
        var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
        var oneDay = 1000 * 60 * 60 * 24;
        var lastTime = new Date(nextMonthFirstDay - oneDay);
        var month = parseInt(lastTime.getMonth() + 1);
        var day = lastTime.getDate();
        if (month < 10) {
            month = '0' + month
        }
        if (day < 10) {
            day = '0' + day
        }
        return date.getFullYear() + '-' + month + '-' + day;
    }

    //时间选择器
    function timePicker(id) {
        api.openPicker({
            type: 'date',
            date: new Date(),
            title: '选择日期'
        }, function (ret, err) {
            if (ret) {
                var monthTemp = (ret.month < 10 ? '0' + ret.month : ret.month);
                var dayTemp = (ret.day < 10 ? '0' + ret.day : ret.day);
                var date = ret.year + "-" + monthTemp + "-" + dayTemp;
                $("#" + id).val(date);
            }
        });
    }

    //查询生产
    function search() {

        //计划日期起
        var planDateStart = $("#planDateStart").val();
        //计划日期止
        var planDateEnd = $("#planDateEnd").val();
        if (planDateStart == null || planDateStart == "" || planDateStart == undefined) {
            api.toast({
                msg: "请选择计划日期起",
                duration: 3000
            });
            return;
        }
        if (planDateEnd == null || planDateEnd == "" || planDateEnd == undefined) {
            api.toast({
                msg: "请选择计划日期止",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/searchWorkOrderListGroup";
        var data = {
            values: {
                planDateStart: planDateStart,
                planDateEnd: planDateEnd,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //成功
                if (myobj.code == "0") {

                    var list = eval(myobj.list);
                    if (list.length == 0) {
                        $("#echartBar").html("");
                        api.toast({
                            msg: "无计划数据",
                            duration: 3000
                        });
                        return;
                    }

                    //2：构造柱状图
                    createBar(list);
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //柱状图
    function createBar(list) {

        //计划数
        var xDataPlanQty = [];
        //未完成数
        var xDataNoComplete = [];
        //已完成数
        var xDataComplete = [];
        var yData = [];

        //遍历
        for (var i = 0; i < list.length; i++) {
            xDataPlanQty.push(parseInt(list[i].planqty));
            xDataNoComplete.push(parseInt(list[i].planqty) - parseInt(list[i].completeqty));
            xDataComplete.push(parseInt(list[i].completeqty));
            yData.push(list[i].code);
        }

        var myChart = echarts.init(document.getElementById('echartBar'));

        var option = {
            legend: {
                data: ['未完成', '已完成',]
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                },
                formatter: function (params) {
                    var relVal = params[0].name;
                    var value = 0;
                    for (var i = 0; i < params.length; i++) {
                        value += params[i].value;
                    }
                    for (var i = 0, l = params.length; i < l; i++) {
                        relVal += '<br/>' + params[i].seriesName + ': ' + params[i].value + ', 百分比 : ' + (100 *
                            parseInt(params[i].value) / parseInt(value)).toFixed(2) + "%";
                    }
                    return relVal;
                },
            },
            grid: {
                left: '3%',
                right: '7%',
                bottom: '1%',
                top: '2%',
                containLabel: true
            },
            xAxis: {
                type: 'value',
                show: false
            },
            yAxis: [{
                type: 'category',
                data: yData,
                axisLabel: {
                    interval: 0
                },
                textStyle: {
                    fontSize: 8 //更改坐标轴文字大小
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: false
                },
                axisLine: {
                    show: false,
                },
            }, {
                type: 'category',
                data: xDataPlanQty,
                axisLabel: {
                    interval: 0
                },
                textStyle: {
                    fontSize: 8 //更改坐标轴文字大小
                },
                axisTick: {
                    show: false
                },
                splitLine: {
                    show: false
                },
                axisLine: {
                    show: false,
                },
            }],
            color: ['#00BFFF', 'orange'],
            series: [{
                type: 'bar',
                stack: '总量',
                name: "未完成",
                barWidth: 10,
                yAxisIndex: 0,
                zlevel: 1,
                data: xDataNoComplete
            }, {
                name: '已完成',
                type: 'bar',
                stack: '总量',
                barWidth: 10,
                yAxisIndex: 0,
                zlevel: 1,
                data: xDataComplete
            }, {
                name: '计划数',
                yAxisIndex: 1,
                type: 'bar',
                data: xDataPlanQty,
                barWidth: 10,
            }]
        };

        myChart.setOption(option, true);
    }

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>